﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.accountrun
{
    public partial class receaddress_edite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitWebControl();
                if (Session["MemberID"] == null)
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：登录超时，请重新登录！");
                }
                if (ChangeHope.WebPage.PageRequest.GetQueryInt("addressId") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("addressId"));
                }
            }
        }

        #region 验证
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.WebControl.Validate(this.txtUserName, "输入收货联系人的姓名", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtMobile, "输入收货联系人的移动电话", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtPhone, "输入收货联系人的固定电话", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtAddress, "输入收货联系人的详细地址", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtZip, "输入收货地址的邮编", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtEmail, "输入收货联系人的固定电话", "isemail", "邮件格式", "该项为必须为邮件格式");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
            //联系方式
            this.ddlProvince.Attributes.Add("onchange", "GetCity('" + this.ddlCity.ClientID + "',this.value);");
            this.ddlCity.Attributes.Add("onchange", "GetCity('" + this.ddlBorough.ClientID + "',this.value);");
            this.lblScript.Text = "GetCity('" + this.ddlProvince.ClientID + "','0');\n";
        }
        #endregion

        protected void BandInfo(int id)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.Member.ReceAddress Bll = new YXShop.BLL.Member.ReceAddress();
            YXShop.Model.Member.ReceAddress model = Bll.GetModelByID(id);
            if (model != null)
            {
                this.txtUserName.Text = model.UserName;
                this.txtMobile.Text = model.Mobile;
                this.txtPhone.Text = model.Phone;
                this.txtAddress.Text = model.Address;
                this.txtEmail.Text = model.Email;
                this.txtZip.Text = model.Zip;
                this.txtConstructionSigns.Text = model.ConstructionSigns;
                this.txtConsignesTime.Text = model.ConsignesTime;
                this.rabState.SelectedValue = model.Stat.ToString();
                this.lblScript.Text += "GetCity('" + this.ddlProvince.ClientID + "','0','" + model.Province + "');\n";
                this.lblScript.Text += "GetCity('" + this.ddlCity.ClientID + "','" + model.Province + "','" + model.City + "');\n";
                this.lblScript.Text += "GetCity('" + this.ddlBorough.ClientID + "','" + model.City + "','" + model.Borough + "');\n";
            }
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Member.ReceAddress bll = new YXShop.BLL.Member.ReceAddress();
            YXShop.Model.Member.ReceAddress model = new YXShop.Model.Member.ReceAddress();
            //用户选择了设置为默认收货地址  但是已经存在了一个默认值
            if (this.rabState.SelectedValue == "1" && bll.checkDefault(Convert.ToInt32(Session["MemberID"].ToString()), 1) > 0)
            {
                this.ltlMsg.Text = "操作错误，只允许存在一个默认收货地址";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }

            if (txtMobile.Text!="")
            {
                if (!ChangeHope.Common.ValidateHelper.IsMobilePhone(txtMobile.Text))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确手机号";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (txtPhone.Text!="")
            {
                if (!ChangeHope.Common.ValidateHelper.IsPhone(txtPhone.Text))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确电话号码";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
           
            if (txtZip.Text!="")
            {
                if (!ChangeHope.Common.ValidateHelper.IsSend(txtZip.Text))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确邮政编码";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            model.UID = Convert.ToInt32(Session["MemberID"].ToString());
            model.UserId = Session["MemberName"].ToString();
            model.UserName = this.txtUserName.Text;
            model.Mobile = this.txtMobile.Text;
            model.Phone = this.txtPhone.Text;
            model.Fax = "";
            model.Province = ChangeHope.WebPage.PageRequest.GetFormString("ctl00$workspace$ddlProvince");
            model.City = ChangeHope.WebPage.PageRequest.GetFormString("ctl00$workspace$ddlCity");
            model.Borough = ChangeHope.WebPage.PageRequest.GetFormString("ctl00$workspace$ddlBorough");
            model.Address = this.txtAddress.Text;
            model.Zip = this.txtZip.Text;
            model.Email = this.txtEmail.Text;
            model.Stat = Convert.ToInt32(this.rabState.SelectedValue);
            model.ConsignesTime = this.txtConsignesTime.Text;
            model.ConstructionSigns = this.txtConstructionSigns.Text;
            if (ChangeHope.WebPage.PageRequest.GetQueryInt("addressId") > 0)
            {
                model.ID = ChangeHope.WebPage.PageRequest.GetQueryInt("addressId");
                bll.Amend(model);
                this.ltlMsg.Text = "更新成功！";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                return;
            }
            else
            {
                bll.Add(model);
                this.ltlMsg.Text = "添加成功！";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
                return;
            }
        }
    }
}
